<!--
 @description  业务模块配置
 @fileName index.vue
 @author zengqiongying
 @created 2022/01/14 16:47:42
-->
<template>
  <div>
    <table-box
      ref="refTable"
      :off-set="-10"
      :data="tableData"
      :table-config="tableConfig"
      :other-filters="otherFilters"
      :other-raw="otherRaws"
      :selected-arry.sync="tableSelected"
      :show-selection="false"
      :show-custom-setting="false"
      :show-custom-combine="false"
      :show-custom-collect="false"
      :table-sortable="false"
      @btnOperate="btnOperate"
    />
    <Edit
      v-if="showEdit"
      :visible.sync="showEdit"
      :detail-data="activeRowData"
      @success="refreshTable"
    />

  </div>
</template>

<script>
import { parseTime } from '@/utils'
import { deleteModular } from '@/api/modular'
import Edit from './Edit'
export default {
  name: 'HelpModularList',
  components: { Edit },
  data() {
    return {
      tableSelected: [],
      otherRaws: {},
      otherFilters: [],
      tableConfig: {
        interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/categoryBusiness/page`,
        // hideSearchBar: true,
        // onlySearchBar: true,
        tabIndex: 0,
        sort: 'createTime',
        order: 'desc',
        apiHeader: {
          'Permission-PerCode': 'modular',
          'Permission-App': 'content-center'
        },
        setting: [
          {
            prop: 'classifyCode',
            label: '业务模块编码',
            minWidth: 150
          },
          {
            prop: 'classifyName',
            label: '业务模块名称',
            minWidth: 150
          },
          {
            prop: 'createBy',
            label: '创建人',
            minWidth: 150
          },
          {
            prop: 'createTime',
            label: '创建时间',
            minWidth: 150,
            type: 'date',
            formatData: ({ createTime }) => {
              return parseTime(createTime)
            }
          },
          {
            prop: 'lastUpdateBy',
            label: '修改人',
            minWidth: 150
          },
          {
            prop: 'lastUpdateTime',
            label: '修改时间',
            minWidth: 150,
            type: 'date',
            formatData: ({ lastUpdateTime }) => {
              return parseTime(lastUpdateTime)
            }
          }
        ],
        btnList: !this.isProduction ? [
          {
            buttonName: '新增',
            optCode: 'add',
            outerFlag: true
          },
          {
            buttonName: '编辑',
            optCode: 'edit',
            outerFlag: true
          },
          {
            buttonName: '删除',
            optCode: 'del',
            outerFlag: true
          }] : null
      },
      activeRowData: '',
      showEdit: false
    }
  },
  computed: {

  },
  created() {

  },
  methods: {
    handleselectionChange(val) {
      this.tableSelected = val
    },
    handleRowClick({ row }) {
      this.$refs.refTable.clearSelection()
      this.$nextTick(() => {
        this.$refs.refTable.toggleRowSelection(row, true)
        this.activeRowData = row || ''
      })
    },
    btnOperate(code) {
      switch (code) {
        case 'add':
          this.handleEdit()
          break
        case 'edit':
          if (!this.singleJudge(this.tableSelected, '编辑', true)) {
            return
          }
          this.handleEdit(this.tableSelected[0])
          break
        case 'del':
          if (!this.singleJudge(this.tableSelected, '删除', true)) {
            return
          }
          this.handleDelete(this.tableSelected[0])
          break
        default :
          break
      }
    },
    // 刷新表格数据
    refreshTable(isInit) {
      this.$refs.refTable.initTableData(isInit ? '' : 'onPage')
    },
    // 编辑/新增
    handleEdit(row) {
      this.activeRowData = row || ''
      this.$nextTick(() => {
        this.showEdit = true
      })
    },
    // 删除处理
    handleDelete(row) {
      if (row.hasCategory) {
        this.$message.warning('该业务模块下含类目结构，不可进行删除')
        return
      }
      this.$confirm('此操作将永久删除当前所选类目，是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const id = this.tableSelected[0].id
        deleteModular(id).then(res => {
          if (res.success === 1) {
            this.$message({
              message: res.msg || '删除成功',
              type: 'success'
            })
            this.refreshTable()
          }
        })
      }).catch(() => {})
    }
  }
}

</script>
<style lang='scss' scoped>

</style>
